//
//  BPTVListTableView.m
//  BPTV
//
//  Created by baipeng on 2018/11/15.
//  Copyright © 2018年 Apple Inc. All rights reserved.
//

#import "BPTVListTableView.h"

@interface BPTVListTableView ()<UITableViewDelegate,UITableViewDataSource>
@property(nonatomic,strong)NSMutableArray *dataSourceArray;
@property(nonatomic,strong)NSDictionary *tvDic;

@end

@implementation BPTVListTableView
#pragma mark - life cyle 1、控制器生命周期
- (instancetype)initWithFrame:(CGRect)frame style:(UITableViewStyle)style{
    if (self == [super initWithFrame:frame style:style]) {
        self.backgroundColor = [[UIColor whiteColor] colorWithAlphaComponent:0.5];
        self.dataSource = self;
        self.delegate = self;
    }
    return self;
}
#pragma mark - 2、不同业务处理之间的方法以
//TODO:重写数据
-(void)setTVRoomListTvNameDic:(NSDictionary *)tvNameDic{
    self.tvDic = tvNameDic;
    self.dataSourceArray = [[NSMutableArray alloc]initWithArray:[self.tvDic.allKeys sortedArrayUsingComparator:^NSComparisonResult(id  _Nonnull obj1, id  _Nonnull obj2) {
        return [obj1 compare:obj2 options:NSBackwardsSearch];
    }]];
    [self reloadData];
}
#pragma mark - Network 3、网络请求

#pragma mark - Action Event 4、响应事件

#pragma mark - Call back 5、回调事件

#pragma mark - Delegate 6、代理、数据源
-(NSInteger)numberOfSectionsInTableView:(UITableView *)tableView{
    return 1;
}
-(NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section{
    return self.dataSourceArray.count;
}
-(UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath{
    static NSString * const identifier = @"cell";
    UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:identifier];
    if (!cell) {
        cell = [[UITableViewCell alloc]initWithStyle:UITableViewCellStyleDefault reuseIdentifier:identifier];
    }
    cell.textLabel.text = self.dataSourceArray[indexPath.row];
    cell.backgroundColor = UIColor.clearColor;
    cell.selectionStyle = UITableViewCellSelectionStyleNone;
    return cell;
}
-(CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath{
    return KScale(30);
}
-(CGFloat)tableView:(UITableView *)tableView heightForHeaderInSection:(NSInteger)section{
    return 0.01;
}
-(CGFloat)tableView:(UITableView *)tableView heightForFooterInSection:(NSInteger)section{
    return 0.01;
}
-(void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath{
    self.hidden = YES;
    if (self.BPTVListTableComBlack) {
        self.BPTVListTableComBlack(self.tvDic[self.dataSourceArray[indexPath.row]]);
    }
}
#pragma mark - interface 7、UI处理

#pragma mark - lazy loading 8、懒加载


-(NSMutableArray *)dataSourceArray{
    if (!_dataSourceArray) {
        NSString *path = [[NSBundle mainBundle] pathForResource:@"tv" ofType:@"plist"];
        _tvDic = [[NSDictionary alloc]initWithContentsOfURL:[NSURL fileURLWithPath:path]];
        _dataSourceArray = [[NSMutableArray alloc]initWithArray:[_tvDic.allKeys sortedArrayUsingComparator:^NSComparisonResult(id  _Nonnull obj1, id  _Nonnull obj2) {
            return [obj1 compare:obj2 options:NSBackwardsSearch];
        }]];
    }
    return _dataSourceArray;
}
/*
// Only override drawRect: if you perform custom drawing.
// An empty implementation adversely affects performance during animation.
- (void)drawRect:(CGRect)rect {
    // Drawing code
}
*/

@end
